package com.qf.commons.web.exception;

import com.qf.commons.core.exception.ServiceException;
import com.qf.commons.data.result.AjaxResult;
import com.qf.commons.data.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: tiger
 * @Data: 2024/10/22 22:42
 * @Description: 全局异常处理器 -Controller的增强 - 处理Controller之后的异常
 **/

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * @MethodName: serviceExceptionHandler
     * @Param: [serviceException]
     * @Return: com.qf.commons.data.result.AjaxResult
     * @Date: 2024/10/22 23:06
     * @Description: 业务异常 - 自己主动抛送的异常
     **/
    @ExceptionHandler(ServiceException.class)
    public AjaxResult serviceExceptionHandler(ServiceException serviceException) {
        log.error("发生了全局的业务异常 - {}", serviceException.getMessage());
        return AjaxResult.fail(serviceException.getCode(), serviceException.getMsg());
    }


    /**
     * @MethodName: globalExceptionHandler
     * @Param: [throwable]
     * @Return: void
     * @Date: 2024/10/22 23:07
     * @Description: 非业务异常 - 意料之外的异常
     **/
    @ExceptionHandler
    public AjaxResult globalExceptionHandler(Throwable throwable) {
        //通过日志记录当前的异常信息
        log.error("发生了全局异常 - {}", throwable);
        return AjaxResult.fail(ResultCode.FAIL);
    }
}
